package org.vacoor.ticket.bean.param.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.vacoor.ticket.bean.Passenger;
import org.vacoor.ticket.bean.Station;
import org.vacoor.ticket.bean.param.Parameter;
import org.vacoor.ticket.handler.impl.BookingResultHandler;
import org.vacoor.ticket.util.DateUtils;

/**
 * 基本订单信息
 * 封装点击预订后获取的信息
 * @author vacoor
 */
public class OrderParam implements Parameter{
	//以下两个属性为得到该订单的结果
	public int stateCode = BookingResultHandler.ERR_UNKNOW;
	private String msg = "未知错误";
	
	//剩余车票详情字符串
	private String remainTicketsDetail;	//剩余车票字符串,12306中的leftTicketStr / ypTicketStr
	private String token;			//TOKEN 防止重复提交
	private Date trainDate;			//日期
	private String trainId;			//列车编号
	private String trainNo;		//车次
	private Station fromStation;	//出发站
	private Station arriveStation;	//目的站
	private String startTime;
	private String endTime;
	
	private String seatTypeCode;
	private String ticketTypeOrderNum;
	private String bedLevelOrderNum;
	private String cancelFlag;
	private String idMode;
	private String reserveFlag;
	
	//获取订单后设置这两个参数
	private List<Passenger> passengers = new ArrayList<Passenger>();
	private String randCode;
	
	public String getRemainTicketsDetail() {
		return remainTicketsDetail;
	}

	public void setRemainTicketsDetail(String remainTicketsDetail) {
		this.remainTicketsDetail = remainTicketsDetail;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public Date getTrainDate() {
		return trainDate;
	}

	public void setTrainDate(Date trainDate) {
		this.trainDate = trainDate;
	}

	public String getTrainId() {
		return trainId;
	}

	public void setTrainId(String trainId) {
		this.trainId = trainId;
	}

	public String getTrainNo() {
		return trainNo;
	}

	public void setTrainNo(String trainNo) {
		this.trainNo = trainNo;
	}

	public Station getFromStation() {
		return fromStation;
	}

	public void setFromStation(Station fromStation) {
		this.fromStation = fromStation;
	}

	public Station getArriveStation() {
		return arriveStation;
	}

	public void setArriveStation(Station arriveStation) {
		this.arriveStation = arriveStation;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getSeatTypeCode() {
		return seatTypeCode;
	}

	public void setSeatTypeCode(String seatTypeCode) {
		this.seatTypeCode = seatTypeCode;
	}

	public String getTicketTypeOrderNum() {
		return ticketTypeOrderNum;
	}

	public void setTicketTypeOrderNum(String ticketTypeOrderNum) {
		this.ticketTypeOrderNum = ticketTypeOrderNum;
	}

	public String getBedLevelOrderNum() {
		return bedLevelOrderNum;
	}

	public void setBedLevelOrderNum(String bedLevelOrderNum) {
		this.bedLevelOrderNum = bedLevelOrderNum;
	}

	public String getCancelFlag() {
		return cancelFlag;
	}

	public void setCancelFlag(String cancelFlag) {
		this.cancelFlag = cancelFlag;
	}

	public String getIdMode() {
		return idMode;
	}

	public void setIdMode(String idMode) {
		this.idMode = idMode;
	}
	
	public String getReserveFlag() {
		return reserveFlag;
	}

	public void setReserveFlag(String reserveFlag) {
		this.reserveFlag = reserveFlag;
	}
	
	public List<Passenger> getPassengers() {
		return passengers;
	}

	public void setPassengers(List<Passenger> passengers) {
		this.passengers = passengers;
	}

	public String getRandCode() {
		return randCode;
	}

	public void setRandCode(String randCode) {
		this.randCode = randCode;
	}

	@Override
	public String toString() {
		return "OrderInfo [stateCode=" + stateCode + ", msg=" + msg
				+ ", remainTicketsDetail=" + remainTicketsDetail + ", token="
				+ token + ", trainDate=" + trainDate + ", trainId=" + trainId
				+ ", trainNo=" + trainNo + ", fromStation=" + fromStation
				+ ", arriveStation=" + arriveStation + ", startTime="
				+ startTime + ", endTime=" + endTime + ", seatTypeCode="
				+ seatTypeCode + ", ticketTypeOrderNum=" + ticketTypeOrderNum
				+ ", bedLevelOrderNum=" + bedLevelOrderNum + ", cancelFlag="
				+ cancelFlag + ", idMode=" + idMode + ", reserveFlag="
				+ reserveFlag + ", passengers=" + passengers + ", randCode="
				+ randCode + "]";
	}

	public int getStateCode() {
		return stateCode;
	}

	public void setStateCode(int stateCode) {
		this.stateCode = stateCode;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	//@Override
	public String getQueryString() {
		return getOrderQueryString() + "&" +
				getPassengerQueryString() +
				//这个好像也不是必须的
				"&checkbox0=0" + 
				"&tFlag=dc";
	}
	
	private String getOrderQueryString() {
		return 
				//
		"org.apache.struts.taglib.html.TOKEN=" + getToken() + 	//TOKEN
		"&leftTicketStr=" + getRemainTicketsDetail() +		//订单号
		"&orderRequest.train_date=" + DateUtils.formatDate(getTrainDate(),"yyyy-MM-dd")+ 
		"&orderRequest.train_no=" + getTrainId() +	//列车编号
		"&orderRequest.station_train_code=" + getTrainNo()	+ //车次
		"&orderRequest.from_station_name=" + getFromStation().getName() +
		"&orderRequest.to_station_name=" + getArriveStation().getName() + 
		"&orderRequest.from_station_telecode=" + getFromStation().getCode() +
		"&orderRequest.to_station_telecode=" + getArriveStation().getCode() + 
		"&orderRequest.start_time=" + getStartTime() +
		"&orderRequest.end_time=" + getEndTime() +
		"&orderRequest.bed_level_order_num=" + getBedLevelOrderNum() +
		"&orderRequest.cancel_flag=" + getCancelFlag() +
		"&orderRequest.ticket_type_order_num=" + getTicketTypeOrderNum() +  
		"&orderRequest.seat_type_code=" + getSeatTypeCode() +
		"&orderRequest.id_mode=" + getIdMode() +
		"&orderRequest.reserve_flag=" + getReserveFlag() +
		"&randCode=" + randCode + 
		"&rand=" + randCode;
	}

	//获取用户信息的查询字符串
	private String getPassengerQueryString() {
		//这些有些是可以不要的,没测试
		StringBuilder sb = new StringBuilder();
		/*
		if(passengers.size() > 0 ) {
			//这个是表示从历史中选择的人,不写没测试
			Passenger p = passengers.get(0);
			sb.append("oldPassengers=" + p.getName() + "," + p.getCardType().getValue() + "," + p.getCardNo());
		}
		*/
		for(int i=0; i<passengers.size();i++) {
			Passenger p = passengers.get(i);
			//从历史记录中选取的人,必须设置？？
			//sb.append("&oldPassengers=" + p.getName() + "," + p.getCardType().getValue() + "," + p.getCardNo());
			sb.append("&passenger_" + (i+1) + "_seat=" + p.getSeatType().getValue());
			sb.append("&passenger_" + (i+1) + "_ticket=" + p.getTicketType().getValue());
			sb.append("&passenger_" + (i+1) + "_name=" + p.getName());
			sb.append("&passenger_" + (i+1) + "_cardtype=" + p.getCardType().getValue());
			sb.append("&passenger_" + (i+1) + "_cardno=" + p.getCardNo());
			sb.append("&passenger_" + (i+1) + "_mobileno=" + p.getMobile());
			//这个是是否保存到12306联系人
			sb.append("&checkbox9=" + (p.isSavePassenger()? "Y" : "N" )); 
			//这个是必须的
			sb.append("&passengerTickets=" + 
					p.getSeatType().getValue() + "," +
					"undefined," +
					p.getTicketType().getValue() + "," +
					p.getName() + "," +
					p.getCardType().getValue() + "," +
					p.getCardNo() + "," +
					p.getMobile() + "," +
					(p.isSavePassenger() ? "Y" : "N"));
		}
		return sb.toString().substring(1);
	}
}

